package org.cangku.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.cangku.dao.*;
import org.cangku.entity.Permission;
import org.cangku.entity.Role;
import org.cangku.entity.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RoleDao roleDao;

    /*登录判断用户是否存在*/
    @RequestMapping("/extend")
    public String extend(String username, String password, HttpServletRequest request){
        //System.out.println(password);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(username,password);

        try {
            subject.login(token);
            /*得到用户的登录的角色id*/
            String pwd = userInfoDao.getPwdByName(username);
            Userinfo info = userInfoDao.login(username,pwd);
            List<Role> roleList = roleDao.getAllRole();
            //得到用户信息存到session中
            request.getSession().setAttribute("userinfo",info);
            request.getSession().setAttribute("rolelist",roleList);
            /*得到该角色所拥有的权限*/
            System.out.println("角色id："+info.getRoleid());
            List<Long> pmid = rolePermissionDao.getPermissionidByroleid(info.getRoleid());
            List<Permission> list = permissionDao.queryByPermissionid(pmid);

            /*存放父节点*/
            List<Permission> list1 = new ArrayList<Permission>();
            for (Permission permission: list){
                if(permission.getParentid() == 0){
                    list1.add(permission);
                }
            }
            System.out.println("asdasdad");
            System.out.println(list1.size());

            request.getSession().setAttribute("listAll",list);
            request.getSession().setAttribute("listfu",list1);

            return "redirect:/ShouYe.jsp";
        } catch (UnknownAccountException e){
            request.getSession().setAttribute("uname",username);
            request.getSession().setAttribute("mima",password);
            request.getSession().setAttribute("msg","用户名不存在");
            return "redirect:/login.jsp";
        } catch (AuthenticationException e) {
            request.getSession().setAttribute("uname",username);
            request.getSession().setAttribute("mima",password);
            request.getSession().setAttribute("msg","密码不正确");
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("userinfo");
        return "redirect:/login.jsp";
    }

    /*查询*/
    @RequestMapping("/query")
    @ResponseBody
    public List<Userinfo> query(){
        return userInfoDao.query() ;
    }

    /*添加*/
    public List<Userinfo> insert(){
        return this.query();
    }

    /*删除*/
    @RequestMapping("/pldelete")
    @ResponseBody
    public Map<String,Object> query(String ids){
        String[] id = ids.split(",");
        for(int i = 0;i<id.length;i++){
            userInfoDao.DeleteByid(Integer.parseInt(id[i]));
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",systemUserDao.getAllUser("",0,0,2));
        map.put("total",systemUserDao.queryUserCount("",0));

        return map;
    }

    /*修改用户角色*/
    @RequestMapping("/updateRole")
    @ResponseBody
    public Map<String,Object> UpdateRole(int roleid,int id){

        userInfoDao.UpdateUserRoleId(roleid,id);

        Map<String,Object> map = new HashMap<String, Object>();

        map.put("rows",systemUserDao.getAllUser("",0,0,2));
        map.put("total",systemUserDao.queryUserCount("",0));

        return map;
    }
}
